﻿using Microsoft.Phone.Shell;
using Tfs.OnTheRoad.Model;
using Tfs.OnTheRoad.Pages;
using Tfs.OnTheRoad.ViewModel;

namespace Tfs_OnTheRoad
{
    /// <summary>
    /// Changeset details page
    /// </summary>
    public partial class ChangesetDetailsPage : BasePage
    {
        public ChangesetDetailsPage()
        {
            InitializeComponent();
        }
        protected override void Refresh()
        {
            if (changesGrid.DataContext != null)
                ((ChangeListViewModel)changesGrid.DataContext).LoadData();

            if (WorkItemsListControl.DataContext != null)
                ((WorkItemListViewModel)WorkItemsListControl.DataContext).LoadData();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            var viewModel = PhoneApplicationService.Current.State["CurrentChangeset"] as Changeset;
            if (viewModel != null)
            {
                if (changesGrid.DataContext == null)
                {
                    ChangeListViewModel changeViewModel = new ChangeListViewModel(viewModel);
                    changesGrid.DataContext = changeViewModel;
                    changeViewModel.LoadData();
                }
                if (WorkItemsListControl.DataContext == null)
                {
                    WorkItemListViewModel workItemsViewModel = new WorkItemListViewModel(viewModel);
                    WorkItemsListControl.DataContext = workItemsViewModel;
                    workItemsViewModel.LoadData();
                }
                this.DataContext = viewModel;
            }
            var currentProject = PhoneApplicationService.Current.State["CurrentProject"] as Project;
            if (currentProject != null)
                HeaderControlPage.NavigationPath = string.Format("{0} \\ Changeset = {1}", currentProject.Name, viewModel.Id);
        }
    }
}
